package org.smart4j.framework.common.util;

import lombok.extern.slf4j.Slf4j;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Objects;
import java.util.Properties;

/**
 * 读取属性文件工具类
 *
 * @author YooLin1c
 * @version 1.0.0
 * @since 2018-01-29 12:51
 */
@Slf4j
public final class PropertiesUtil {
    private static Properties props;

    public static Properties loadProps(String propertiesPath) {
        props = new Properties();
        try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertiesPath)) {
            if (Objects.isNull(in)) {
                throw new FileNotFoundException("Couldn't found file ".concat(propertiesPath));
            }
            props.load(in);
        } catch (Exception e) {
            log.error("Load file ".concat(propertiesPath).concat(" failed"), e);
        }
        return props;
    }

    public static int getInt(Properties properties, String key) {
        return CastUtil.castInt(properties.getProperty(key));
    }

    public static int getInt(Properties properties, String key, int defaultValue) {
        return CastUtil.castInt(properties.getProperty(key, CastUtil.castString(defaultValue)));
    }
}
